//
//  TopDetailViewController.m
//  WXMovie
//
//  Created by apple on 16/4/10.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "TopDetailViewController.h"

@interface TopDetailViewController ()

@property(nonatomic, strong)NSMutableArray *commentArr;
@property(nonatomic, strong)NSIndexPath *selectIndexPath;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation TopDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createView];
    [self loadData];
    
}

-(void)loadData {
//    @property (nonatomic,copy)NSString *image;
//    @property (nonatomic,copy)NSString *titleCn;
//    @property (nonatomic, copy)NSString *directors;
//    @property (nonatomic, copy)NSString *actors;
//    @property (nonatomic, copy)NSString *type;
//    @property (nonatomic, strong)NSDictionary *time;
//    @property (nonatomic,strong)NSArray *videos;
    
    NSDictionary *detailDic = [WXDataService requestDataWithJsonFile:@"movie_detail.json"];
    TopHeaderModel *detailModel = [[TopHeaderModel alloc] initWithDic:detailDic];
    detailModel.image = detailDic[@"image"];
    detailModel.titleCn = detailDic[@"titleCn"];
    detailModel.directors = detailDic[@"directors"][0];
    detailModel.actors = detailDic[@"actors"][0];
    detailModel.type = detailDic[@"type"][0];
    detailModel.time = detailDic[@"release"];
//    detailModel.videos = detailDic[@"vidoes"];
    headerView.model = detailModel;
    
//    NSLog(@"%@", headerView.model);
//    NSLog(@"%@", detailDic);

    _commentArr = [NSMutableArray array];
    NSDictionary *commentDic = [WXDataService requestDataWithJsonFile:@"movie_comment.json"];
    NSArray *commentArray = commentDic[@"list"];
   
    for (NSDictionary *dic in commentArray) {
        TopCommentModel *model = [[TopCommentModel alloc] initWithDic:dic];
        [_commentArr addObject:model];
    }

    
}

-(void)createView {
    
    headerView = [[[NSBundle mainBundle] loadNibNamed:@"TopHeaderView" owner:self options:nil] firstObject];
    self.tableView.tableHeaderView = headerView;
    self.tableView.separatorColor = [UIColor colorWithWhite:0.32 alpha:1];
    [self.tableView registerNib:[UINib nibWithNibName:@"TopCommentCell" bundle:nil] forCellReuseIdentifier:@"TopCommentCell"];
    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TopCommentCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.bgView.layer.cornerRadius = 8;
    
    cell.model = self.commentArr[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.selectIndexPath isEqual:indexPath]) {
        
        TopCommentModel *model = self.commentArr[indexPath.row];
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineBreakMode = NSLineBreakByWordWrapping;
        
        CGRect frame = [model.content boundingRectWithSize:CGSizeMake(kScreenWidth - 114, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{
                                                                                                                                                           
                                     NSFontAttributeName : [UIFont systemFontOfSize:16],
                                     NSParagraphStyleAttributeName : style
                                     
                                     }
                        context:nil];
        
        CGFloat height = MAX(frame.size.height + 49 + 10 , 90);
        return height;
    }else{
        return 90;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![_selectIndexPath isEqual:indexPath]) {
        self.selectIndexPath = indexPath;
        
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}



- (void)didReceiveMemoryWarning {
   
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
